/**
 * class  FreeSprite
 * 自定义Sprite
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import as3.component.Component;
	import as3.component.event.KeyEnterEvent;
	import as3.component.geom.IntRectangle;
	
	[Event(name="change", type="flash.events.Event")]
	
	[Event(name="enter", type="as3.component.event.KeyEnterEvent")]
	
	[Event(name="focusIn", type="flash.events.FocusEvent")]
	
	[Event(name="focusOut", type="flash.events.FocusEvent")]
	
	public class TextArea extends Component
	{
		
		protected var _activeTextColor:uint;
		protected var _enableTextColor:uint;
		
		public var _textField:Label;
		
		protected var _textBox:DisplayObject;
		protected var _textBoxFocus:DisplayObject;
		protected var _textBoxEnabled:DisplayObject;
		
		private var _defaultWidth:Number = 100;
		private var _defaultHeight:Number = 26;
		
		private var _hPadding:Number = 21;
		private var _vPadding:Number = 4;
		
		private var _editable:Boolean = true;
		
		private var _enabled:Boolean = true;
		
		public var value:Number = 21;
		
		protected var _defaultTextFormat:TextFormat;
		
		protected var _verticalScrollBar:UIScrollBar;
		
		protected var _verticalScrollPolicy:String = "auto";
		
		protected var _needScroll:Boolean = false;
		
		public function TextArea()
		{
			super();
		}
		
		override protected function initUI():void
		{
			super.initUI();
//			_uiLibrary["textBox"] = "TextInput_textBoxSkin";
//			_uiLibrary["textBoxFocus"] = "TextInput_textBoxFocusSkin";
//			_uiLibrary["textBoxEnabled"] = "TextInput_textBoxEnabledSkin";
			
			_activeTextColor = _defaultSkin.textInputCssTable.activeTextColor || _activeTextColor || 0x000000;
			_enableTextColor = _defaultSkin.textInputCssTable.enableTextColor || _enableTextColor || 0x666666;
			_activeTextColor = 0;
		}
		
		override protected function initCom():void
		{
			super.initCom();
			setMasked(true);
			_textField = new Label();
			_textField.selectable = false;
			_textField.color = _activeTextColor;
			_textField.textField.type = TextFieldType.INPUT;
			_textField.textField.multiline = true;
			_textField.textField.wordWrap = true;
			_textField.width = _width = _defaultWidth - (2 * _hPadding);
			_textField.height = _height = _defaultHeight - (2 * _vPadding);
			_textField.color = 0xffffff;
			_verticalScrollBar = new UIScrollBar();
			_verticalScrollBar.x = _textField.x - value;
			_verticalScrollBar.y = _textField.y;
			_verticalScrollBar.height = _textField.height;
			_verticalScrollBar.scrollTarget = _textField.textField;
			addChild(_textField);
			reLayout();
		}
		
		override protected function initListener():void
		{
			_textField.addEventListener(FocusEvent.FOCUS_IN, $onFocusInHandler);
			_textField.addEventListener(FocusEvent.FOCUS_OUT, $onFocusOutHandler);
			_textField.textField.addEventListener(Event.CHANGE, $onChangeHandler);
		}
		
		public function set textBox(dis:DisplayObject):void
		{
			if (_textBox)
			{
				if (contains(_textBox)) removeChild(_textBox);
			}
			_textBox = dis;
//			propertyChanged = true;
			reSize();
		}
		
		public function set textBoxFocus(dis:DisplayObject):void
		{
			if (_textBoxFocus)
			{
				if (contains(_textBoxFocus)) removeChild(_textBoxFocus);
			}
			_textBoxFocus = dis;
//			propertyChanged = true;
			reSize();
		}
		
		public function set textBoxEnabled(dis:DisplayObject):void
		{
			if (_textBoxEnabled)
			{
				if (contains(_textBoxEnabled)) removeChild(_textBoxEnabled);
			}
			_textBoxEnabled = dis;
//			propertyChanged = true;
			reSize();
		}
		
		public function set displayAsPassword(value:Boolean):void
		{
			_textField.textField.displayAsPassword = value;
		}
		
		public function get displayAsPassword():Boolean
		{
			return _textField.textField.displayAsPassword;
		}
		
		protected function reSize():void
		{
			
			if (_textBox){
				_textBox.width = _width;
				_textBox.height = _height;
				_textBox.x = 1;
				_textBox.y = 1;
				addChildAt(_textBox, 0);
			}
			
			if (_textBoxFocus){
				_textBoxFocus.width = _width + 2;
				_textBoxFocus.height = _height + 2;
				_textBoxFocus.x = _textBoxFocus.y = 0;
				addChildAt(_textBoxFocus, 0);
				_textBoxFocus.visible = false;
			}
			
			if (_textBoxEnabled){
				_textBoxEnabled.width = _width;
				_textBoxEnabled.height = _height;
				_textBoxEnabled.x = 1;
				_textBoxEnabled.y = 1;
				addChildAt(_textBoxEnabled, 0);
				_textBoxEnabled.visible = false;
			}
			_textField.x = _hPadding;
			_textField.y = _vPadding;
			_textField.height = _height - (2 * _vPadding) + 2;
			if (needVScroll()) {
				if (!contains(_verticalScrollBar)) addChild(_verticalScrollBar);
				_textField.width = _width - (2 * _hPadding) + 2 - _verticalScrollBar.width;
				_verticalScrollBar.x = _textField.x - value;
				_verticalScrollBar.y = _textField.y;
				_verticalScrollBar.height = _textField.height;
			} else {
				if (contains(_verticalScrollBar)) removeChild(_verticalScrollBar);
				_textField.width = _width - (2 * _hPadding) + 2;
			}
			setMaskRect(new IntRectangle(0,0,_width + 2, _height + 2));
		}
		
		override public function set width(value:Number):void
		{
			if (_width == value) return;
			
			_width = value;
			reSize();
//			_verticalScrollBar.update();
		}
		
		override public function set height(value:Number):void
		{
			if (_height == value) return;
			
			_height = value;
			reSize();
//			_verticalScrollBar.update();
		}
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		
		public function set verticalScrollPolicy(value:String):void
		{
			if (_verticalScrollPolicy == value) return;
			if (value == ScrollBarPolicy.AUTO || value == ScrollBarPolicy.OFF || value == ScrollBarPolicy.ON)
			{
				_verticalScrollPolicy = value;
			}
			reLayout();
		}
		
		protected function needVScroll() : Boolean
		{
			if (_verticalScrollPolicy == ScrollBarPolicy.OFF)
			{
				return false;
			}
			if (_verticalScrollPolicy == ScrollBarPolicy.ON)
			{
				return true;
			}
			return _textField.textField.maxScrollV > 1;
		}
		
		protected function reLayout():void
		{
			var b:Boolean = needVScroll();
			if (b == _needScroll) return;
			_needScroll = b;
			if (_needScroll) {
				if (!contains(_verticalScrollBar)) addChild(_verticalScrollBar);
				_textField.width = _width - (2 * _hPadding) + 2 - _verticalScrollBar.width;
				_textField.height = _height - (2 * _vPadding) + 2;
				_verticalScrollBar.x = _textField.x - value;
				_verticalScrollBar.y = _textField.y;
				_verticalScrollBar.height = _textField.height;
			} else {
				if (contains(_verticalScrollBar)) removeChild(_verticalScrollBar);
				_textField.width = _width - (2 * _hPadding) + 2;
				_textField.height = _height - (2 * _vPadding) + 2;
			}
			
		}
		
		protected function $onFocusInHandler(evt:FocusEvent):void
		{
			if (_textBoxFocus && enabled && editable) {
				_textBoxFocus.visible = true;
				_textBox.visible = false;
			}
			dispatchEvent(evt);
		}
		
		protected function $onFocusOutHandler(evt:FocusEvent):void
		{
			if (_textBoxFocus) {
				_textBoxFocus.visible = false;
				_textBox.visible = true;
			}
			dispatchEvent(evt);
		}
		
		protected function $onChangeHandler(evt:Event):void
		{
			dispatchEvent(evt);
			reLayout();
		}
		
		public function set text(value:String):void
		{
			_textField.text = value;
			reLayout();
		}
		
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set htmlText(value:String):void
		{
			_textField.htmlText = value;
			reLayout();
		}
		
		public function get htmlText():String
		{
			return _textField.htmlText;
		}
		
		public function set editable(value:Boolean):void
		{
			if (_editable == value) return;
			_editable = value;
			if (_editable) {
				_textField.textField.type = TextFieldType.INPUT;
			}
			else  {
				_textField.textField.type = TextFieldType.DYNAMIC;
			}
		}
		
		public function get editable():Boolean
		{
			return _editable;
		}
		
		public function set enabled(value:Boolean):void
		{
			if (_enabled == value) return;
			_enabled = value;
			if (_enabled)
			{
				_textBox.visible = true;
				_textBoxEnabled.visible = false;
				_textField.color = _activeTextColor;
				if (_editable) {
					_textField.textField.type = TextFieldType.INPUT;
					_textField.selectable = true;
				}
				else  {
					_textField.textField.type = TextFieldType.DYNAMIC;
					_textField.selectable = false;
				}
			} else {
				_textField.selectable = false;
				_textBox.visible = false;
				_textBoxEnabled.visible = true;
				_textField.textField.type = TextFieldType.DYNAMIC;
				_textField.color = _enableTextColor;
			}
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		
		public function getTextField():TextField
		{
			return _textField.textField;
		}
		
		public function getLabel():Label
		{
			return _textField;
		}
		
		public function set _mouseEnabled(bool:Boolean):void
		{
			_textField.mouseChildren = bool;
			_textField.mouseEnabled = bool;
		}
		
		public function getScrollBar():UIScrollBar
		{
			return _verticalScrollBar;
		}
		
		public function hideScrollBar(value:Boolean):void
		{
			_verticalScrollBar.visible = !value;
		}
	}
}